const mysql = require('mysql2/promise'),
      { con, ResData } = require('./common');
      
/*查询所有管理员*/
async function getAllAdmins(){
  let res = new ResData();
  let sql = `select id, admin_name, admin_tel, admin_email, admin_role, admin_avatar from admins;`;
  var [rows] = await con.execute(sql);
  res.data = rows;
  res.count = rows.length;
  return res;
};
/*管理员账号查询管理员信息*/
async function getAAdmin(admin_account){
  let res = new ResData();
  let sql = `select admin_name, admin_role, admin_avatar
from admins
where admin_account = '${admin_account}';`;
  var [rows] = await con.execute(sql);
  res.data = rows;
  res.count = rows.length;
  return res;
};
/*判断登录*/
async function ifLogin(admin_account, password){
  console.log(admin_account, password);
  let res = new ResData();
  let sql = `select password from admins where admin_account = '${admin_account}' and password='${password}'`;
  console.log(sql);
  try{
    var [rows] = await con.execute(sql);
    rows.length === 1 ? res.data = true : res.data = false;
    console.log(rows.length);
  }catch(e){
    res.code = 4014005;
    res.msg = 'login failed'
  }
  return res;
};
module.exports = {
  getAllAdmins,
  ifLogin,
  getAAdmin
};
